package com.yxhl.zoume.core.car.fragment;


import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import com.bigkoo.convenientbanner.ConvenientBanner;
import com.bigkoo.convenientbanner.holder.CBViewHolderCreator;
import com.bigkoo.convenientbanner.listener.OnItemClickListener;
import com.yxhl.zoume.R;
import com.yxhl.zoume.ZouMeApplication;
import com.yxhl.zoume.common.ui.fragment.BaseFragment;
import com.yxhl.zoume.core.busticket.presenter.BusTicketHomePresenter;
import com.yxhl.zoume.core.busticket.view.BannerObtionView;
import com.yxhl.zoume.core.busticket.view.NetworkImageHolderView;
import com.yxhl.zoume.core.specialcar.model.SpecialCarContainerEntrance;
import com.yxhl.zoume.core.specialcar.model.SpecialCarContainerActivityParam;
import com.yxhl.zoume.core.main.ui.MainActivity;
import com.yxhl.zoume.data.DataConstants;
import com.yxhl.zoume.data.http.model.splashbanner.BannerBean;
import com.yxhl.zoume.di.component.splash.DaggerSplashBannerComponent;
import com.yxhl.zoume.di.module.ActivityModule;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * created by sugarya 16/08/21
 */
public class CarHomeFragment extends BaseFragment implements BannerObtionView, OnItemClickListener {

    private static final String TAG = "CarHomeFragment";

    @BindView(R.id.ll_fragment_home_special_car)
    LinearLayout mSpecialCarLl;

    @BindView(R.id.ll_fragment_home_express_car)
    LinearLayout mExpressCarLl;

    @BindView(R.id.ll_fragment_home_official_car)
    LinearLayout mOfficialCarLl;
    // 顶部Banner
    @BindView(R.id.convenientBanner)
    ConvenientBanner convenientBanner;

    // 轮播间隔
    private static final long AUTO_TURNING_TIME = 5000;
    List<String> networkImageURLs = new ArrayList<>();
    private List<BannerBean> bannerList;
    private boolean isBannerClick = false;
    @Inject
    BusTicketHomePresenter busTicketHomePresenter;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // 绑定view
        busTicketHomePresenter.attachView(this);
        busTicketHomePresenter.obtainBanner(convenientBanner);
        processImageList();
    }

    private void processImageList() {
        String[] images = {DataConstants.BUS_TICKET_HOME_BANNER_1, DataConstants.BUS_TICKET_HOME_BANNER_2};
        for (int i = 0; i < images.length; i++) {
            networkImageURLs.add(images[i]);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = super.onCreateView(inflater, container, savedInstanceState);
        return view;
    }

    @Override
    public void onResume() {
        super.onResume();
        busTicketHomePresenter.onResume();
        showBannerImages();
        convenientBanner.startTurning(AUTO_TURNING_TIME);
    }

    @Override
    public void onPause() {
        super.onPause();
        busTicketHomePresenter.onPause();
        convenientBanner.stopTurning();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        busTicketHomePresenter.onDestroy();
    }

    public CarHomeFragment() {
        // Required empty public constructor
    }

    public static CarHomeFragment newInstance() {

        Bundle args = new Bundle();

        CarHomeFragment fragment = new CarHomeFragment();
        fragment.setArguments(args);
        return fragment;
    }


    @OnClick(R.id.ll_fragment_home_special_car)
    void onSpecialCarClick() {
        ZouMeApplication.application.getAppComponent().navigator()
                .navigateToSpecialCarContainerActivity(
                        mActivity,
                        new SpecialCarContainerActivityParam(SpecialCarContainerEntrance.SPECIAL_CAR_HOME));
    }

    @OnClick(R.id.ll_fragment_home_express_car)
    void onExpressCarClick() {
        ZouMeApplication.application.getAppComponent().navigator().navigateToExpressCarContainerActivity(mActivity);
    }

    @OnClick(R.id.ll_fragment_home_official_car)
    void onOfficialCarClick() {
        ZouMeApplication.application.getAppComponent().navigator().navigateToOfficialCarContainerActivity(mActivity);
    }


    @Override
    protected void initializeInjector() {
        DaggerSplashBannerComponent
                .builder()
                .activityModule(new ActivityModule(mActivity))
                .appComponent(getAppComponent())
                .build()
                .inject(this);
    }

    @Override
    protected int getLayoutResId() {
        return R.layout.fragment_car_home;
    }

    @Override
    protected void setupToolBar() {
        if (mActivity instanceof MainActivity) {
            MainActivity mainActivity = (MainActivity) mActivity;
            mainActivity.setToolbarTitle(getString(R.string.main_tab_name_car));
            ((MainActivity) mActivity).setToolbarVisible(true);
        }
    }

    @Override
    public void renderBannerOnSuccess(List<BannerBean> bannerList) {
        this.bannerList = bannerList;
        isBannerClick = true;
        networkImageURLs.clear();
        for (BannerBean bannerbean : bannerList) {
            networkImageURLs.add(bannerbean.getImgUrl());
        }
        showBannerImages();
    }

    @Override
    public void renderBannerOnFailure(String failureMsg) {
        showBannerImages();
    }

    @Override
    public void renderBannerOnError(String errorMsg) {
        showBannerImages();
    }

    /**
     * 展示Banner轮播图
     */
    public void showBannerImages() {
        if (null == convenientBanner) {
            return;
        }
        networkImageURLs.size();
        convenientBanner.setPages(new CBViewHolderCreator<NetworkImageHolderView>() {
            @Override
            public NetworkImageHolderView createHolder() {
                return new NetworkImageHolderView();
            }
        }, networkImageURLs)
                .setPageIndicator(new int[]{R.drawable.ic_page_indicator, R.drawable.ic_page_indicator_focused})
                .setOnItemClickListener(this);
    }

    @Override
    public void onItemClick(int position) {
        if (isBannerClick) {
            if (null != bannerList) {
                String targetUrl = bannerList.get(position).getTargetUrl();
                if (!TextUtils.isEmpty(targetUrl)) {
                    getAppComponent().navigator().navigateToAdActivity(mActivity, targetUrl );
                }
            }
        }
    }
}
